#include "Container.h"
#include "Control.h"
#include "ContainerPainter.h"
#include "../debug_new.h"

CContainer::CContainer(HWND hWnd)
{
	m_pHotControl		= NULL;
	m_pLastHotControl	= NULL;
	m_clrBK				= ::GetSysColor(COLOR_DESKTOP);
	m_hBmpBackgnd		= NULL;
	m_vecControls.clear();
	m_hWnd				= hWnd;
	m_pPainter			= new CContainerPainter();
	m_nAniType			= 1;
	m_nExitAniType		= 1;
	m_nAniSpeed			= 0;
	m_fZoom=1.00;
}

CContainer::~CContainer()
{
	if(m_pPainter)
	{
		delete m_pPainter;
	}

	if(m_hBmpBackgnd)
	{
		DeleteObject(m_hBmpBackgnd);
	}

	RemoveAllControls();
}

void CContainer::RemoveAllControls()
{
	int nCount = m_vecControls.size();

	for (int i = 0; i < nCount; i++)
	{
		delete m_vecControls.at(i);
	}

	m_vecControls.clear();
}

void CContainer::RemoveControl(int nID)
{
	CControl*	pControl = NULL;

	int nCount = m_vecControls.size();

	for(int i=0; i < nCount; i++)
	{
		if(m_vecControls.at(i)->m_nID == nID)
		{
			pControl = m_vecControls.at(i);
			if(m_pLastHotControl && m_pLastHotControl->m_nID == nID)
			{
				m_pLastHotControl = NULL;
			}
	   		m_vecControls.erase(m_vecControls.begin()+i);

			delete pControl;
			break;
		}
	}
}

CControl* CContainer::GetHotControl()
{
	return m_pHotControl;
}

void CContainer::SetZoom(float fZoom)
{
	m_fZoom=fZoom;
}

float  CContainer::GetZoom()
{
	return m_fZoom;
}

CControl* CContainer::AddControl(CControl* pControl)
{
	if(pControl)
	{
		pControl->SetContainer(this);
		m_vecControls.push_back(pControl);

		return pControl;
	}
	return NULL;
}

CControl* CContainer::ControlFromPoint(CPoint point)
{
	CControl*	pControl = NULL;

	int nCount = m_vecControls.size();
	for(int i=nCount-1; i >=0; i--)
	{
		if(m_vecControls.at(i)->GetRect().PtInRect(point))
		{
			pControl = m_vecControls.at(i);
			if((pControl->GetStyle() & SWS_STATIC) || !pControl->IsVisible() )
			{
				pControl = NULL;
				continue;
			}
			break;
		}
	}

	if(NULL != pControl && NULL != m_pHotControl)
	{

		if(!(m_pHotControl->GetState()&STATE_DISABLED))
		{
			m_pHotControl->SetState(STATE_NORMAL);
		}

		//ningwei amend 090727
	/*	if( pControl != m_pHotControl )
		{
			CClientDC dc(m_hWnd);
			m_pHotControl->Paint(&dc);
		}*/

		m_pLastHotControl = m_pHotControl;
	}

	return pControl;
}

CControl* CContainer::ControlFromID(int nID)
{
	CControl*	pControl = NULL;

	int nCount = m_vecControls.size();
	for(int i=0; i < nCount; i++)
	{
		if(m_vecControls.at(i)->m_nID == nID)
		{
			pControl = m_vecControls.at(i);
			break;
		}
	}

	return pControl;
}

void CContainer::OnLButtonDown(UINT nFlags, CPoint point)
{
	CControl*	pControl = NULL;
	pControl = ControlFromPoint(point);
	if(pControl)
	{
		if(pControl->IsEnabled() && pControl->IsVisible())
		{
			m_pHotControl = pControl;
			m_pHotControl->Click(CLICK_DOWN, point);
		}
	}
	else
	{
		m_pHotControl = NULL;
	}

}

void CContainer::OnLButtonUp(UINT nFlags, CPoint point)
{
	if(NULL == m_pHotControl || !m_pHotControl->IsEnabled() || !m_pHotControl->IsVisible() )
	{
		return;
	}

	if(m_pHotControl->GetRect().PtInRect(point))
	{
		m_pHotControl->Click(CLICK_UP, point);

		if(NULL != m_pLastHotControl && m_pLastHotControl != m_pHotControl)
		{
			CClientDC dc(m_hWnd);
			m_pLastHotControl->Paint(&dc);
		}
		m_pHotControl = NULL;
	}
	else
	{
		// leon 09-05-12 comment for progress flicking
		CClientDC dc(m_hWnd);
		m_pHotControl->SetState(STATE_NORMAL);
		m_pHotControl->Paint(&dc);
		m_pHotControl = NULL;
	}
	g_ID = 0;
}

void  CContainer::OnMouseMove(UINT nFlags, CPoint point)
{

}

void CContainer::SetPainter(CContainerPainter *pPainter)
{
	if(NULL != m_pPainter)
	{
		delete m_pPainter;
	}
	m_pPainter = pPainter;
}

void CContainer::OnPaint(CDC *pDC)
{
	if(NULL != m_pPainter)
		m_pPainter->Paint(pDC,this,m_rcRect);
	else
	{
		Paint( pDC );
	}
}

void CContainer::Paint(CDC *pDC)
{
	if(NULL != m_pPainter)
		m_pPainter->Paint(pDC,this,m_rcRect);
	else
	{
		PaintBackGround( pDC->m_hDC,NULL );
		int nCount = m_vecControls.size();
		for(int i=0; i < nCount; i++)
			m_vecControls.at(i)->Paint( pDC );
	}
}

void  CContainer::PaintBackGround( HDC hdc,LPRECT prc )
{
	if ( m_hBmpBackgnd==NULL || hdc==NULL )
		return;
	
	CDC dcTmp;
	dcTmp.CreateCompatibleDC(NULL);
	HBITMAP hBmpOld = dcTmp.SelectBitmap( GetBackGnd() );
	if ( prc!=NULL )
	{
		BitBlt( hdc,prc->left,prc->top,(prc->right-prc->left),(prc->bottom-prc->top),
							dcTmp.m_hDC,prc->left,prc->top,SRCCOPY);
	}
	else
	{
		BitBlt( hdc,0,0,m_rcRect.Width(),m_rcRect.Height(),dcTmp.m_hDC,0,0,SRCCOPY);
	}

	if ( NULL!=hBmpOld )
	{
		dcTmp.SelectBitmap(hBmpOld);
	}
	dcTmp.DeleteDC();

}